﻿using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace FrontEnd.Controllers;

[Route("api/[controller]")]
[ApiController]
public class DaprBindingsController : ControllerBase
{
    private readonly ILogger<DaprBindingsController> _logger;
    public DaprBindingsController(ILogger<DaprBindingsController> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// 输入绑定
    /// </summary>
    /// <returns></returns>
    [HttpPost("Input")]
    public async Task<ActionResult> InputAsync()
    {
        byte[] buffer = new byte[Request.ContentLength.Value];
        using Stream stream = Request.Body;
        stream.Position = 0L;
        await stream.ReadAsync(buffer, 0, buffer.Length);
        string content = Encoding.UTF8.GetString(buffer);
        _logger.LogInformation($"{DateTime.Now} ,binding(InputAsync) ===> { content }");
        return Ok();
    }

    /// <summary>
    /// 输出绑定
    /// operation 操作类型：create，get，delete，list
    /// </summary>
    /// <param name="daprClient"></param>
    /// <returns></returns>
    [HttpGet("Output")]
    public async Task<ActionResult> OutputAsync([FromServices] DaprClient daprClient,string data)
    {
        _logger.LogInformation($"{DateTime.Now} ,binding(OutputAsync) ===> {data}");
        await daprClient.InvokeBindingAsync("api/DaprBindings/Output", "create", $"{DateTime.Now} ===> {data}");
        return Ok();
    }
}

